package com.wedo.mall.activity.login;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;

import com.e.library.activity.EActivity;
import com.e.library.entity.User;
import com.e.library.http.EApi;
import com.e.library.http.EClient;
import com.e.library.http.EObserver;
import com.e.library.http.EResponse;
import com.e.library.utils.EUtils;
import com.wedo.mall.R;
import com.wedo.mall.activity.ProtocolActivity;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindString;
import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observable;

/**
 * @author lxy
 * @time 2018/2/6  18:25
 */
public class BindingActivity extends EActivity{

    @BindView(R.id.et_username) EditText et_username;
    @BindView(R.id.et_check_code) EditText et_check_code;
    @BindView(R.id.btn_check_code) Button btn_check_code;
    @BindView(R.id.et_phone) EditText et_phone;
    @BindString(R.string.register_vc_resend) String register_vc_resend;

    @Override
    protected int layout() {
        return R.layout.activity_binding;
    }

    @Override
    protected void onActivityCreated(Bundle state) {
    }

    @OnClick(R.id.btn_protocol)
    protected void onProtocolClick() {
        toActivity(ProtocolActivity.class);
    }

    /**获取验证码*/
    @OnClick(R.id.btn_check_code)
    protected void onCheckCodeClick(){

        String account = et_username.getText().toString();
        if(!validation(account)) {
            return;
        }
        loading();
        Observable<EResponse> observable = EClient.with().api().verifyCode(account,"3");
        EClient.with().observer(observable, new EObserver() {
            @Override
            public void successful(EResponse response) {
                mTimer.start();
                dismiss(R.string.register_vc_send);
            }

            @Override
            public void failure(String msg) {
                dismiss(msg);
            }
        });

    }

    /**绑定*/
    @OnClick(R.id.btn_binding)
    protected void onBindingClick(){
        final String account = et_username.getText().toString();
        if(!validation(account)) {
            return;
        }

        String code = et_check_code.getText().toString();
        if(TextUtils.isEmpty(code)) {
            toast(R.string.register_vc_hint);
            return;
        }

        Map<String,String> params = new HashMap<>(4);
        params.put("email",account);
        params.put("verify",code);

        String phone = et_phone.getText().toString().trim();
        if (!TextUtils.isEmpty(phone)){
            params.put("phone",phone);
        }

        loading();
        Observable<EResponse> observable = EClient.with().api().post(EApi.LOGIN_BING,params);
        EClient.with().observer(observable, new EObserver() {
            @Override
            public void successful(EResponse response) {
                dismiss();
                User user = WLogin.user();
                user.nickname = account;
                WLogin.saveUser(BindingActivity.this,user);
                defaultFinish();
            }

            @Override
            public void failure(String msg) {
                dismiss(msg);
            }
        });

    }

    /**验证注册账号*/
    private boolean validation(String account){
        if (TextUtils.isEmpty(account)){
            toast(R.string.register_email_enter);
            return false;
        }

        if (!EUtils.isEmail(account)){
            toast(R.string.register_email_invalid);
            return false;
        }

        return true;
    }

    private CountDownTimer mTimer = new CountDownTimer(60*1000,1000) {
        @Override
        public void onTick(long millis) {
            if (isDestroyed()) return;
            btn_check_code.setClickable(false);
            btn_check_code.setText(String.format(register_vc_resend,millis/1000));
        }

        @Override
        public void onFinish() {
            if (isDestroyed()) return;
            btn_check_code.setClickable(true);
            btn_check_code.setText(R.string.register_get_vc);

        }
    };

    @Override
    public void onDestroy() {
        mTimer.cancel();
        mTimer = null;
        super.onDestroy();
    }

}
